﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Table_Dal;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class QuantriDal:DataAccessLayer
    {
        

        public QuantriDal()
        {
            
        }
        #region Function
        DataDataContext dt;
        public bool KiemTra_TrungKhoa(string Ma)
        {
            dt = GetKetnoi();
            IEnumerable<QUANTRI> q = dt.QUANTRIs.Where(k => k.MADANGNHAP == Ma);
            foreach (QUANTRI item in q)
            {
                if (item.MADANGNHAP == Ma)
                    return true;
            }
            return false;
        }
        public QuanTriTbl LogIn(string TenDangNhap)
        {
            
            QuanTriTbl user = new QuanTriTbl();
            dt = GetKetnoi();
           IEnumerable< QUANTRI >qt = dt.QUANTRIs.Where(i => i.MADANGNHAP == TenDangNhap);
            
           foreach (QUANTRI item in qt)
           {
               user.MADANGNHAP = item.MADANGNHAP;
               user.MATKHAU = item.MATKHAU;
               user.PHANQUYEN = int.Parse(item.PHANQUYEN.ToString());
           }
            return user;
        }
        public bool ThemQuanTri(QuanTriTbl Qtri)
        {

            try
            {
                dt = GetKetnoi();
                QUANTRI qt = new QUANTRI();
                qt.MADANGNHAP = Qtri.MADANGNHAP;
                qt.MATKHAU = Qtri.MATKHAU;
                qt.PHANQUYEN = Qtri.PHANQUYEN;
                dt.QUANTRIs.InsertOnSubmit(qt);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        
        public bool Xoa(string MaDangNhap)
        {
            try
            {

                DataDataContext Context = GetKetnoi();
                QUANTRI qt = Context.QUANTRIs.SingleOrDefault(i => i.MADANGNHAP == MaDangNhap);
                Context.QUANTRIs.DeleteOnSubmit(qt);
                Context.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool Sua(QuanTriTbl quantri)
        {
            try
            {

                DataDataContext Context = GetKetnoi();
                QUANTRI qt = Context.QUANTRIs.SingleOrDefault(i => i.MADANGNHAP == quantri.MADANGNHAP);
                qt.MATKHAU = quantri.MATKHAU;
                qt.PHANQUYEN = quantri.PHANQUYEN;
                Context.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public int PhanQuyen(string MaDangNhap)
        {
            DataDataContext Context = GetKetnoi();
            QUANTRI qt = Context.QUANTRIs.SingleOrDefault(i => i.MADANGNHAP == MaDangNhap);
            return int.Parse(qt.PHANQUYEN.ToString());
        }
        public string MatKhau(string user)
        {
            DataDataContext context = GetKetnoi();
            QUANTRI qt = context.QUANTRIs.SingleOrDefault(i => i.MADANGNHAP == user);
            return qt.MATKHAU;
        }
        public bool ThayDoiMatKhau(string MaDangNhap, string MatKhauMoi)
        {
            DataDataContext ct = GetKetnoi();
            QUANTRI qt = ct.QUANTRIs.SingleOrDefault(i => i.MADANGNHAP == MaDangNhap);
            qt.MATKHAU = MatKhauMoi;
            try
            {
                ct.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        
        #endregion

        public QuanTriTbl Thongtin_User(string ma)
        {
            QuanTriTbl tbl = null;
            dt = GetKetnoi();
            try
            {
                QUANTRI qt = dt.QUANTRIs.SingleOrDefault(k => k.MADANGNHAP == ma);
                tbl = new QuanTriTbl();
                tbl.MATKHAU = qt.MATKHAU;
                tbl.MADANGNHAP = qt.MADANGNHAP;
            }
            catch { }
            return tbl;
        }
    }
}
